package com.zprmb.udzt.adapter;

import java.text.DecimalFormat;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zprmb.udzt.R;
import com.zprmb.udzt.V6BBtDetailsActivity;
import com.zprmb.udzt.bean.ProductZT;
import com.zprmb.udzt.util.BitmapUtils;
import com.zprmb.udzt.util.Regular;

public class V6BbtAdapter extends BaseAdapter {
	private Context context;
	private List<ProductZT> list;
	 DecimalFormat df = new DecimalFormat("###.00");  
	 DecimalFormat df2 = new DecimalFormat("###0.0");
	public V6BbtAdapter(Context context, List<ProductZT> list) {
		super();
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@SuppressLint("ViewHolder")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHelpBbt h;
		if(convertView==null){
			h=new ViewHelpBbt();
			convertView=LayoutInflater.from(context).inflate(R.layout.v6_bbt_list_item, null);
			h.tv_name=(TextView)convertView.findViewById(R.id.conduct_name);
			h.tv_time=(TextView)convertView.findViewById(R.id.conduct_time);
			h.tv_rate=(TextView)convertView.findViewById(R.id.conduct_rate);
			h.tv_rateadd=(TextView)convertView.findViewById(R.id.home_rateadd);
			h.tv_investAmount=(TextView)convertView.findViewById(R.id.conduct_investAmount);
			h.tv_newuser=(TextView)convertView.findViewById(R.id.tv_new_user);
			h.li_jiaxi=(LinearLayout)convertView.findViewById(R.id.li_jiaxi);
			h.img_list_status=(ImageView)convertView.findViewById(R.id.img_list_status);
			h.img_pl_reward=(TextView)convertView.findViewById(R.id.img_pl_reward);
			h.tv_red=(TextView)convertView.findViewById(R.id.tv_new_user);
//			h.tv_coupon=(TextView)convertView.findViewById(R.id.conduct_addrate);
			h.btn_invest=(Button)convertView.findViewById(R.id.btn_invest);
			
			
			
			convertView.setTag(h);	
		}else{
			h=(ViewHelpBbt) convertView.getTag();
		}
//		h.tv_coupon.setVisibility(View.INVISIBLE);
		h.img_pl_reward.setVisibility(View.INVISIBLE);
		if(list.get(position).getLoanTitle().indexOf("闪电标")==0){
			h.tv_red.setVisibility(View.GONE);
		}else{
			
			h.tv_red.setVisibility(View.VISIBLE);
//			h.tv_newuser.setText("可用红包");
		}
		convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(context,V6BBtDetailsActivity.class);				 
				intent.putExtra("method",list.get(position).getMethod());
				intent.putExtra("name", list.get(position).getLoanTitle());				
				intent.putExtra("id", list.get(position).getId());
				intent.putExtra("type", list.get(position).getProductType());
				context.startActivity(intent);
			}
		});
		h.tv_name.setText(list.get(position).getLoanTitle());
		if (list.get(position).getRateAdd().equals("0")) {
			list.get(position).setVIS(true);
		}
		if(list.get(position).isVIS()){
			h.tv_rateadd.setVisibility(View.INVISIBLE);
		}else{
			h.tv_rateadd.setVisibility(View.VISIBLE);
		}
		if(list.get(position).getRateAdd().equals("0")){
			h.li_jiaxi.setVisibility(View.GONE);
			h.tv_rate.setText(df.format(Double.parseDouble(list.get(position).getOriginRate())/100));
		}else{
			h.li_jiaxi.setVisibility(View.VISIBLE);
			
			h.tv_rate.setText(df.format(Double.parseDouble(list.get(position).getOriginRate())/100));
			h.tv_rateadd.setText(df2.format(Double.parseDouble(list.get(position).getRateAdd())/100)+"%");
			}
		switch (list.get(position).getStatus()) {
		
        case "CLEARED":
        	h.img_list_status.setVisibility(View.VISIBLE);
           h.img_list_status.setImageBitmap(BitmapUtils.readBitMap(context, R.drawable.yhq2));
            break;
        case "FINISHED":
        	h.img_list_status.setVisibility(View.VISIBLE);
        	h.img_list_status.setImageBitmap(BitmapUtils.readBitMap(context, R.drawable.ysq));
            break;
        case "SETTLED":
        	h.img_list_status.setVisibility(View.VISIBLE);
        	h.img_list_status.setImageBitmap(BitmapUtils.readBitMap(context, R.drawable.ysq));
            break;
       
        default:
        	h.img_list_status.setVisibility(View.INVISIBLE);
    }
		 
		if(list.get(position).getMonths().equals("0")){
			if(!list.get(position).getYears().equals("0")){
				if(!list.get(position).getDays().equals("0")){
					h.tv_time.setText(Integer.parseInt(list.get(position).getYears())*12+"个月"+list.get(position).getDays()+"天");
				}else{
					
					h.tv_time.setText(Integer.parseInt(list.get(position).getYears())*12+"个月");
				}
			}else{
				
				h.tv_time.setText(list.get(position).getDays()+"天");
			}
		}else{
			if(list.get(position).getDays().equals("0")){
				if(!list.get(position).getYears().equals("0")){
					h.tv_time.setText((Integer.parseInt(list.get(position).getYears())*12)+Integer.parseInt(list.get(position).getMonths())+"个月");
					
				}else{
					h.tv_time.setText(+Integer.parseInt(list.get(position).getMonths())+"个月");
				}
			}else{
				if(!list.get(position).getYears().equals("0")){
					h.tv_time.setText((Integer.parseInt(list.get(position).getYears())*12)+Integer.parseInt(list.get(position).getMonths())+"个月"+list.get(position).getDays()+"天");
				}else{
					
					h.tv_time.setText(list.get(position).getMonths()+"个月"+list.get(position).getDays()+"天");
				}
			}
		}
		
		h.tv_investAmount.setText(Regular.getDecimalFormatTwo((Double.parseDouble(list.get(position).getAmount())-Double.parseDouble(list.get(position).getInvestAmount()))/10000)+"");
		
//		if(list.get(position).getStatus().equals("OPENED")){
//			
//			h.btn_invest.setText("马上投资");
//			h.btn_invest.setBackgroundResource(R.drawable.my_button);
//		}else if(list.get(position).getStatus().equals("SETTLED")){
//			h.btn_invest.setText("还款中");
//			h.btn_invest.setBackgroundResource(R.drawable.btn_unclick);
//		}else{
//			h.btn_invest.setText("已售罄");
//			h.btn_invest.setBackgroundResource(R.drawable.btn_unclick);
//		}
		return convertView;
	}
   class ViewHelpBbt{
	   private TextView tv_name,tv_coupon,tv_rate,tv_rateadd,tv_time,tv_investAmount,tv_newuser,tv_coupon_type;
	   private LinearLayout li_jiaxi,li_newuser;
	   private ImageView img_list_status;
	   private TextView tv_red,		img_pl_reward;
	   private Button btn_invest;
   }
}
